public class MyCirCleQueue {
    //采用标记的方法,判断队列为满或者队列为空
    private int[] elem;

    private int front;

    private int rear;

    private boolean tag;

    public MyCirCleQueue(int k) {
        elem = new int[k];
    }

    //入队
    public void offer(int val) {
        if(isFull()) {
            throw new RuntimeException("队列满，无法入队！");
        }
        elem[rear] = val;
        rear = (rear + 1) % elem.length;
        tag = true;
    }


    //出队
    public int poll() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空，无法出队！");
        }
        elem[front] = 0;
        int value = elem[front];
        front = (front + 1) % elem.length;
        tag = false;
        return value;
    }

    //判空
    public boolean isEmpty() {
        return rear == front && !tag;
    }

    //判满
    public boolean isFull() {
        return rear == front && tag;
    }

    public int peek() {
        if(isEmpty()) {
            return -1;
        }

        if(rear == 0) {
            return elem[elem.length - 1];
        }
        return elem[rear - 1];
    }

    public int front() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }
 }
